package com.huayue.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.huayue.entity.User;

/**
 * 拦截器
 * 
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            User loginUser = (User)request.getSession().getAttribute("USER");
            if (loginUser != null || loginUser.getId() != 0) {
                return true;
            } else {
                response.sendRedirect("/index.jsp");
                return false;
            }
        } catch (Exception e) {
            response.sendRedirect("/index.jsp");
            return false;
        }
    }
}
